package com.xbongbong.pro.message.config;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.pro.message.service.MessageSetService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息服务配置
 *
 * @author LiZhikang
 * @date 2022/11/1 16:37
 */

@Configuration
public class MessageServiceConfig implements ApplicationContextAware {

    private Map<String, MessageSetService> messageSetMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.messageSetMap = applicationContext.getBeansOfType(MessageSetService.class);
    }

/**
     * 消息设置
     *
     * @return
     */

    @Bean("messageSetMap")
    public Map<String, MessageSetService> messageSetMap() {
        Map<String, MessageSetService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.messageSetMap.forEach((key, item) -> {
            item.alias().forEach(alias -> resultMap.put(alias, item));
        });
        return resultMap;
    }

}
